package com.cdtu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cdtu.entity.SysDept;
import com.cdtu.entity.SysEmpRole;
import com.cdtu.entity.SysEmployee;
import com.cdtu.entity.SysRole;
import com.cdtu.service.SysDeptService;
import com.cdtu.service.SysEmpRoleService;
import com.cdtu.service.SysEmployeeService;
import com.cdtu.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : Sakura
 * @Date : 2023/8/8 16:31
 * @Description : TODO 用来接收emp的请求
 */
@RestController
@RequestMapping("/emp")
//解决跨域问题
@CrossOrigin
public class SysEmploymentController extends BaseController{

    @Autowired
    private SysEmpRoleService sysEmpRoleService;

    @Autowired
    private SysEmployeeService sysEmployeeService;

    @Autowired
    private SysDeptService deptService;

    @Autowired
    private SysRoleService roleService;

    @RequestMapping("/list")
    public ModelAndView empList(ModelAndView modelAndView) {

        List<SysDept> deptList = deptService.list();
        //返回数据
        modelAndView.addObject("deptList", deptList);

        List<SysEmployee> list = sysEmployeeService.list();
        //返回数据
        modelAndView.addObject("sysEmpList", list);
        //2.跳转页面;
        modelAndView.setViewName("system/sysEmp/empList");
        return modelAndView;
    }

    @RequestMapping("/delete/{empId}")
    public ModelAndView delete(@PathVariable("empId") Long empId, ModelAndView modelAndView) {

        boolean remove = sysEmployeeService.removeById(empId);
        modelAndView.setViewName("redirect:/emp/list");
        return modelAndView;
    }

    @RequestMapping("/addEmp")
    public ModelAndView addEmpUI(SysEmployee sysEmployee, ModelAndView modelAndView) {

        sysEmployeeService.save(sysEmployee);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/emp/list");
        return modelAndView;
    }

    @RequestMapping("/toUpdateUI/{empId}")
    public ModelAndView toUpdateUi(ModelAndView modelAndView,@PathVariable("empId") Long empId) {

        SysEmployee employee = sysEmployeeService.getById(empId);
        //返回数据
        modelAndView.addObject("employee", employee);

        LambdaQueryWrapper<SysEmpRole> empRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        empRoleLambdaQueryWrapper.eq(SysEmpRole::getEmpId, employee.getId());
        SysEmpRole sysEmpRole = empRoleService.getOne(empRoleLambdaQueryWrapper);
        modelAndView.addObject("sysEmpRole", sysEmpRole);

        getDeptAndRoleList(modelAndView);

        //2.跳转页面;
        modelAndView.setViewName("system/sysEmp/updateSysUser");
        return modelAndView;
    }

    @RequestMapping("/toAddUI")
    public ModelAndView toAddSysEmp(ModelAndView modelAndView) {

        getDeptAndRoleList(modelAndView);



        //2.跳转页面;
        modelAndView.setViewName("system/sysEmp/addSysEmp");
        return modelAndView;
    }

    private void getDeptAndRoleList(ModelAndView modelAndView) {
        List<SysDept> list = deptService.list();
        //将部门数据传送到前端给添加人员选择
        List<SysDept> collect = list.stream().filter(item -> {
            return item.getfDeptId() != 0;
        }).collect(Collectors.toList());

        //返回数据
        modelAndView.addObject("deptList", collect);

        //将角色列表查询出来传送到前端给前端人员选择
        List<SysRole> roleList = roleService.list();
        modelAndView.addObject("roleList", roleList);
    }

    @Autowired
    private SysEmpRoleService empRoleService;
    @Transactional
    @RequestMapping("/update")
    public ModelAndView update(ModelAndView modelAndView,SysEmployee employee,String roleId) {
        sysEmployeeService.updateById(employee);

        LambdaQueryWrapper<SysEmpRole> empRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        empRoleLambdaQueryWrapper.eq(SysEmpRole::getEmpId, employee.getId());
        empRoleService.remove(empRoleLambdaQueryWrapper);

        SysEmpRole sysEmpRole = new SysEmpRole();
        sysEmpRole.setEmpId(employee.getId());
        sysEmpRole.setRoleId(Long.valueOf(roleId));
        empRoleService.save(sysEmpRole);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/emp/list");
        return modelAndView;
    }
}
